<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册 - XunAI 聊天系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.8.0/font/bootstrap-icons.css" rel="stylesheet">
    <style>
        :root {
            --primary-color: #8b5cf6;
            --primary-light: #a78bfa;
            --primary-dark: #7c3aed;
            --primary-bg: #f5f3ff;
            --primary-border: #ddd6fe;
        }

        body {
            background: linear-gradient(135deg, var(--primary-color) 0%, var(--primary-dark) 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
            margin: 0;
            padding: 20px;
            transition: background 0.3s ease;
        }

        .register-container {
            background: white;
            border-radius: 16px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            padding: 2.5rem;
            width: 100%;
            max-width: 450px;
            position: relative;
            overflow: hidden;
            transition: all 0.3s ease;
        }

        .register-container::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 4px;
            background: linear-gradient(90deg, var(--primary-color), var(--primary-light));
        }

        .register-header {
            text-align: center;
            margin-bottom: 2rem;
            position: relative;
        }

        .theme-toggle {
            position: absolute;
            top: 0;
            right: 0;
            background: var(--primary-bg);
            border: none;
            border-radius: 50%;
            width: 40px;
            height: 40px;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            transition: all 0.3s ease;
            color: var(--primary-color);
        }

        .theme-toggle:hover {
            background: var(--primary-color);
            color: white;
            transform: rotate(15deg);
        }

        .register-logo {
            font-size: 2.5rem;
            color: var(--primary-color);
            margin-bottom: 0.5rem;
        }

        .register-title {
            font-weight: 700;
            color: #1f2937;
            margin-bottom: 0.5rem;
        }

        .register-subtitle {
            color: #6b7280;
            font-size: 0.95rem;
        }

        .form-control {
            border-radius: 8px;
            padding: 0.75rem 1rem;
            border: 1px solid #e5e7eb;
            transition: all 0.2s ease;
        }

        .form-control:focus {
            border-color: var(--primary-color);
            box-shadow: 0 0 0 3px rgba(139, 92, 246, 0.1);
        }

        .form-label {
            font-weight: 500;
            color: #374151;
            margin-bottom: 0.5rem;
        }

        .input-group-text {
            background: var(--primary-bg);
            border: 1px solid #e5e7eb;
            border-right: none;
            color: var(--primary-color);
        }

        .form-control.border-start-0 {
            border-left: none;
        }

        .btn-primary {
            background-color: var(--primary-color);
            border-color: var(--primary-color);
            border-radius: 8px;
            padding: 0.75rem;
            font-weight: 500;
            transition: all 0.2s ease;
        }

        .btn-primary:hover {
            background-color: var(--primary-dark);
            border-color: var(--primary-dark);
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(139, 92, 246, 0.3);
        }

        .form-footer {
            margin-top: 1.5rem;
            text-align: center;
            padding-top: 1.5rem;
            border-top: 1px solid #e5e7eb;
            font-size: 0.9rem;
            color: #6c757d;
        }

        .form-footer a {
            color: var(--primary-color);
            text-decoration: none;
            font-weight: 500;
            transition: color 0.2s ease;
        }

        .form-footer a:hover {
            color: var(--primary-dark);
        }

        .password-strength {
            height: 4px;
            margin-top: 8px;
            border-radius: 2px;
            transition: all 0.3s ease;
            background: #e5e7eb;
        }

        .strength-indicator {
            height: 100%;
            border-radius: 2px;
            transition: all 0.3s ease;
        }

        .strength-weak {
            width: 33%;
            background: linear-gradient(90deg, #ef4444, #f87171);
        }

        .strength-medium {
            width: 66%;
            background: linear-gradient(90deg, #f59e0b, #fbbf24);
        }

        .strength-strong {
            width: 100%;
            background: linear-gradient(90deg, #10b981, #34d399);
        }

        .strength-text {
            font-size: 0.85rem;
            margin-top: 4px;
            font-weight: 500;
        }

        .strength-weak-text {
            color: #ef4444;
        }

        .strength-medium-text {
            color: #f59e0b;
        }

        .strength-strong-text {
            color: #10b981;
        }

        .form-check-input:checked {
            background-color: var(--primary-color);
            border-color: var(--primary-color);
        }

        .form-check-label a {
            color: var(--primary-color);
            text-decoration: none;
        }

        .form-check-label a:hover {
            color: var(--primary-dark);
        }

        .alert {
            border-radius: 8px;
            border: none;
            padding: 0.75rem 1rem;
        }

        .alert-danger {
            background-color: #fef2f2;
            color: #dc2626;
            border-left: 4px solid #dc2626;
        }

        .alert-success {
            background-color: #f0fdf4;
            color: #16a34a;
            border-left: 4px solid #16a34a;
        }

        /* 深色主题支持 */
        body.dark-theme {
            background: linear-gradient(135deg, #1a1a1a 0%, #2d2d2d 100%);
        }

        body.dark-theme .register-container {
            background: #2d2d2d;
            color: #e9ecef;
        }

        body.dark-theme .register-title {
            color: #f9fafb;
        }

        body.dark-theme .register-subtitle {
            color: #a0a0a0;
        }

        body.dark-theme .form-control {
            background-color: #1a1a1a;
            border-color: #404040;
            color: #e9ecef;
        }

        body.dark-theme .form-control:focus {
            background-color: #1a1a1a;
            border-color: var(--primary-color);
            color: #e9ecef;
        }

        body.dark-theme .form-label {
            color: #e9ecef;
        }

        body.dark-theme .input-group-text {
            background: #374151;
            border-color: #404040;
            color: var(--primary-light);
        }

        body.dark-theme .form-footer {
            border-top-color: #404040;
            color: #a0a0a0;
        }

        body.dark-theme .password-strength {
            background: #374151;
        }

        body.dark-theme .alert-danger {
            background-color: #7f1d1d;
            color: #fca5a5;
        }

        body.dark-theme .alert-success {
            background-color: #064e3b;
            color: #6ee7b7;
        }

        body.dark-theme .theme-toggle {
            background: #374151;
            color: var(--primary-light);
        }

        body.dark-theme .theme-toggle:hover {
            background: var(--primary-color);
            color: white;
        }

        /* 响应式设计 */
        @media (max-width: 480px) {
            .register-container {
                padding: 1.5rem;
            }

            .register-logo {
                font-size: 2rem;
            }

            .theme-toggle {
                width: 36px;
                height: 36px;
            }
        }
    </style>
</head>
<body>
<div class="register-container">
    <div class="register-header">
        <!-- 主题切换按钮 -->
        <button class="theme-toggle" id="themeToggle" title="切换主题">
            <i class="bi bi-sun" id="themeIcon"></i>
        </button>

        <div class="register-logo">
            <i class="bi bi-robot"></i>
        </div>
        <h3 class="register-title">XunAI 聊天系统</h3>
        <p class="register-subtitle">创建您的账户</p>
    </div>

    <!-- 错误提示 -->
    <div th:if="${param.error}" class="alert alert-danger alert-dismissible fade show mb-4" role="alert">
        <i class="bi bi-exclamation-triangle me-2"></i>
        <span th:text="${param.error}"></span>
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>

    <!-- 成功提示 -->
    <div th:if="${param.success}" class="alert alert-success alert-dismissible fade show mb-4" role="alert">
        <i class="bi bi-check-circle me-2"></i>
        <span th:text="${param.success}"></span>
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>

    <!-- 注册表单 -->
    <!-- 修改：action路径从 /user/register 改为 /auth/register -->
    <form id="registerForm" th:action="@{/auth/register}" method="post">
        <div class="mb-3">
            <label for="username" class="form-label">用户名</label>
            <div class="input-group">
                <span class="input-group-text">
                    <i class="bi bi-person"></i>
                </span>
                <input type="text" class="form-control border-start-0" id="username" name="username" required
                       placeholder="请输入用户名（3-20位字母、数字或下划线）">
            </div>
            <div class="form-text text-muted">用户名将用于登录和显示</div>
        </div>

        <div class="mb-3">
            <label for="email" class="form-label">电子邮箱</label>
            <div class="input-group">
                <span class="input-group-text">
                    <i class="bi bi-envelope"></i>
                </span>
                <input type="email" class="form-control border-start-0" id="email" name="email" required
                       placeholder="请输入电子邮箱">
            </div>
        </div>

        <div class="mb-3">
            <label for="phone" class="form-label">手机号</label>
            <div class="input-group">
                <span class="input-group-text">
                    <i class="bi bi-phone"></i>
                </span>
                <input type="tel" class="form-control border-start-0" id="phone" name="phone"
                       placeholder="请输入手机号（可选）">
            </div>
        </div>

        <div class="mb-3">
            <label for="password" class="form-label">密码</label>
            <div class="input-group">
                <span class="input-group-text">
                    <i class="bi bi-lock"></i>
                </span>
                <input type="password" class="form-control border-start-0" id="password" name="password" required
                       placeholder="请输入密码（至少6位）" oninput="checkPasswordStrength()">
            </div>
            <div class="password-strength">
                <div class="strength-indicator" id="passwordStrength"></div>
            </div>
            <div class="strength-text" id="strengthText">密码强度：弱</div>
        </div>

        <div class="mb-3">
            <label for="confirmPassword" class="form-label">确认密码</label>
            <div class="input-group">
                <span class="input-group-text">
                    <i class="bi bi-lock-fill"></i>
                </span>
                <input type="password" class="form-control border-start-0" id="confirmPassword" name="confirmPassword" required
                       placeholder="请再次输入密码">
            </div>
        </div>

        <div class="mb-3 form-check">
            <input type="checkbox" class="form-check-input" id="agreeTerms" required>
            <label class="form-check-label" for="agreeTerms">
                我已阅读并同意 <a href="#" onclick="showTerms()">服务条款</a>
            </label>
        </div>

        <button type="submit" class="btn btn-primary w-100">
            <i class="bi bi-person-plus me-2"></i>注册账户
        </button>
    </form>

    <!-- 页脚信息 -->
    <div class="form-footer">
        <p>已有账户？<a th:href="@{/auth/login}">立即登录</a></p>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<script>
    // 检查密码强度
    function checkPasswordStrength() {
        const password = document.getElementById('password').value;
        const strengthBar = document.getElementById('passwordStrength');
        const strengthText = document.getElementById('strengthText');

        // 重置
        strengthBar.className = 'strength-indicator';
        strengthText.textContent = '密码强度：弱';
        strengthText.className = 'strength-text strength-weak-text';

        if (password.length === 0) {
            strengthBar.style.width = '0%';
            return;
        }

        let strength = 0;

        // 长度检查
        if (password.length >= 8) strength++;

        // 包含小写字母
        if (/[a-z]/.test(password)) strength++;

        // 包含大写字母
        if (/[A-Z]/.test(password)) strength++;

        // 包含数字
        if (/[0-9]/.test(password)) strength++;

        // 包含特殊字符
        if (/[^a-zA-Z0-9]/.test(password)) strength++;

        // 根据强度设置样式
        if (strength <= 2) {
            strengthBar.classList.add('strength-weak');
            strengthText.textContent = '密码强度：弱';
            strengthText.className = 'strength-text strength-weak-text';
        } else if (strength <= 4) {
            strengthBar.classList.add('strength-medium');
            strengthText.textContent = '密码强度：中';
            strengthText.className = 'strength-text strength-medium-text';
        } else {
            strengthBar.classList.add('strength-strong');
            strengthText.textContent = '密码强度：强';
            strengthText.className = 'strength-text strength-strong-text';
        }
    }

    // 显示服务条款
    function showTerms() {
        alert('服务条款内容将在这里显示\n\n1. 用户需遵守相关法律法规\n2. 不得发布违法或不良信息\n3. 保护用户隐私是我们的责任\n4. 详细条款请参考完整文档');
        return false;
    }

    // 客户端验证
    document.getElementById('registerForm').addEventListener('submit', function(event) {
        const username = document.getElementById('username').value;
        const email = document.getElementById('email').value;
        const password = document.getElementById('password').value;
        const confirmPassword = document.getElementById('confirmPassword').value;
        const agreeTerms = document.getElementById('agreeTerms').checked;

        // 用户名格式验证
        if (!/^[a-zA-Z0-9_]{3,20}$/.test(username)) {
            event.preventDefault();
            alert('用户名格式不正确（3-20位字母、数字或下划线）');
            return;
        }

        // 邮箱格式验证
        if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
            event.preventDefault();
            alert('请输入有效的电子邮箱地址');
            return;
        }

        // 密码长度验证
        if (password.length < 6) {
            event.preventDefault();
            alert('密码长度至少为6位');
            return;
        }

        // 密码确认验证
        if (password !== confirmPassword) {
            event.preventDefault();
            alert('两次输入的密码不一致');
            return;
        }

        // 服务条款同意验证
        if (!agreeTerms) {
            event.preventDefault();
            alert('请同意服务条款');
            return;
        }
    });

    // 页面加载时初始化主题
    document.addEventListener('DOMContentLoaded', function() {
        // 初始化主题
        initTheme();

        // 添加主题切换事件监听
        document.getElementById('themeToggle').addEventListener('click', toggleTheme);
    });

    // 初始化主题
    function initTheme() {
        const savedTheme = localStorage.getItem('theme') || 'light';
        setTheme(savedTheme);
    }

    // 设置主题
    function setTheme(theme) {
        const themeIcon = document.getElementById('themeIcon');

        if (theme === 'dark') {
            document.body.classList.add('dark-theme');
            themeIcon.className = 'bi bi-moon-stars';
            themeIcon.parentElement.title = '切换到浅色主题';
        } else {
            document.body.classList.remove('dark-theme');
            themeIcon.className = 'bi bi-sun';
            themeIcon.parentElement.title = '切换到深色主题';
        }

        localStorage.setItem('theme', theme);
    }

    // 切换主题
    function toggleTheme() {
        const currentTheme = localStorage.getItem('theme') || 'light';
        const newTheme = currentTheme === 'light' ? 'dark' : 'light';
        setTheme(newTheme);
    }
</script>
</body>
</html>